package com.realbrand.net.realbrandpay.ui.activity.gradevin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.adapter.CommAdapter;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.bean.GradevinDetailsBean;
import com.realbrand.net.realbrandpay.bean.ShopcarBean;
import com.realbrand.net.realbrandpay.ui.activity.ADSActivity;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.CommodityDetailsActivity;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.NetworkImageHolderView;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.ReportActivity;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.app.CaptureActivity;
import com.realbrand.net.realbrandpay.zxing.util.L;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/4.
 */

public class GradevinDetailActivity extends BaseActivity {
    @BindView(R.id.commodityDetails_tv_commodity)
    TextView commodityDetailsTvCommodity;
    @BindView(R.id.commodityDetails_tv_money)
    TextView commodityDetailsTvMoney;
    @BindView(R.id.commodityDetails_tv_storeName)
    TextView commodityDetailsTvStoreName;
    @BindView(R.id.commodityDetails_tv_storeAdress)
    TextView commodityDetailsTvStoreAdress;
    @BindView(R.id.commodityDetails_tv_day)
    TextView commodityDetailsTvDay;
    @BindView(R.id.commodityDetails_tv_makeAdress)
    TextView commodityDetailsTvMakeAdress;
    @BindView(R.id.commodityDetails_tv_report)
    TextView commodityDetailsTvReport;
    @BindView(R.id.commodityDetails_toolBar)
    Toolbar commodityDetailsToolBar;
    @BindView(R.id.commodityDetails_tv_scan)
    TextView commodityDetailsTvScan;
    @BindView(R.id.commodityDetails_scrollView)
    ScrollView commodityDetailsScrollView;
    @BindView(R.id.commodityDetails_iv_scanState)
    ImageView commodityDetailsIvScanState;
    @BindView(R.id.commodityDetails_cBanner_commodity)
    ConvenientBanner commodityDetailsCBannerCommodity;
    @BindView(R.id.commodityDetails_lv_commodityProfile)
    ListView commodityDetailsLvCommodityProfile;
    @BindView(R.id.commodityDetails_bt_map)
    Button commodityDetailsBtMap;


    private GradevinDetailsBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        commodityDetailsToolBar.setTitle("");
        commodityDetailsToolBar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(commodityDetailsToolBar);

        //获取传过来的对象
        Intent intent = getIntent();
        bean = (GradevinDetailsBean) intent.getExtras().getSerializable("bean");
//        commodityDetailsIvCommodity.setImageResource(bean.getDrawableId());
//        Picasso.with(this).load(bean.getDrawableId()).into(commodityDetailsIvCommodity);
        String[] split = bean.getMoreInfo().split(",");
        List<String> list = new ArrayList<>();
        for (int j = 1; j < split.length; j++) {
            list.add(split[0] + split[j]);
        }
        L.d("listView有多少项" + list.size() + list.toString());
        commodityDetailsLvCommodityProfile.setAdapter(new CommAdapter(this, list));
        fixListViewHeight(commodityDetailsLvCommodityProfile);
        String[] strings = bean.getDrawableId().split(",");
        String[] imgs = new String[strings.length - 1];
        for (int i = 1; i < strings.length; i++) {
            imgs[i - 1] = strings[0] + strings[i];
        }

        commodityDetailsCBannerCommodity.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, Arrays.asList(imgs))
                .setPointViewVisible(true)
                .startTurning(3000)
                .setPageIndicator(new int[]{R.drawable.switching1, R.drawable.switching})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
//        commodityDetailsBvCommodity.setViewList(listImg);
//        commodityDetailsBvCommodity.startLoop(true);

        commodityDetailsTvCommodity.setText(bean.getName());
        commodityDetailsTvMakeAdress.setText(bean.getProductionAdress());
        commodityDetailsTvDay.setText(bean.getActivationTime());
        commodityDetailsTvMoney.setText(bean.getPrice() + "");
        commodityDetailsTvStoreAdress.setText(bean.getAdress());
        commodityDetailsTvStoreName.setText(bean.getStore());
        commodityDetailsTvScan.setText(bean.getGoodsScan());
        commodityDetailsToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setOnClick();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.commoditydetails_activity;
    }

    @Override
    protected void setOnClick() {
        commodityDetailsTvReport.setOnClickListener(this);
        commodityDetailsBtMap.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commodityDetails_tv_report:
                Intent intent = new Intent(this, ReportActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("report", bean);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.commodityDetails_bt_map:
                break;
        }

    }

    private void fixListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        int totalHeight = 0;
        // 如果没有设置数据适配器，则ListView没有子项，返回
        if (listAdapter == null) {
            return;
        }
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            View listViewItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listViewItem.measure(0, 0);
            // 计算所有子项的高度和
            totalHeight += listViewItem.getMeasuredHeight();
        }
        //设置布局参数
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // listView.getDividerHeight()获取子项间分隔符的高度
        listView.setDividerHeight(0);
        /*分割线高度设置成0，在Theme.NoTitleBar主题下，不设置成0会默认画一条黑色的分割线，并且点击item背景变黄色，将主题改为Theme.Holo.Light.NoActionBar,则分割线正常且点击item背景变灰色*/
        // params.height设置ListView完全显示需要的高度
//      params.height = totalHeight ;
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
